package com.jtcl.module.controller;

import com.jtcl.common.utils.JacksonUtils;
import com.jtcl.module.pojo.User;
import io.swagger.annotations.Api;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 首页
 *
 * @author 施锦庭
 * @since 2020-12-28 20:23
 **/
@RestController
@Api("登录及首页Rest")
public class HomeController {

    @GetMapping(path = "/index")
    public String getIndexPage() {
        // 认证成功后,结果user信息会通过SecurityContextHolder存入SecurityContext中.
        Object obj = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (obj instanceof User) {
            return JacksonUtils.writeValueAsString(obj);
        }
        return "/index";
    }

    @GetMapping(path = "/discuss")
    public String getDiscussPage() {
        return "/site/discuss";
    }

    @GetMapping(path = "/letter")
    @Secured(value = "user")
    public String getLetterPage() {
        return "/site/letter";
    }

    @GetMapping(path = "/admin")
    @Secured(value = "admin")
    public String getAdminPage() {
        return "/site/admin";
    }

    @RequestMapping(path = "/loginpage", method = {RequestMethod.GET, RequestMethod.POST})
    public String getLoginPage() {
        return "/site/login";
    }


    @GetMapping(path = "/denied")
    public String getDeniedPage() {
        return "/error/404";
    }

}

